package com.jz.community.moduleshoppingguide.nearshop.ui.adapter;

import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.NearShopCategoryBean;

import java.util.List;


/**
 * Created by Ani_H on 2018/3/9.
 */

public class DownstairTypeAdapter extends BaseQuickAdapter<NearShopCategoryBean, BaseViewHolder> {


    private onItemClickListener clickListener;

    public DownstairTypeAdapter(int layoutResId, @Nullable List<NearShopCategoryBean> data) {
        super(layoutResId, data);
        mData = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, NearShopCategoryBean entity) {

        RelativeLayout main_ll = helper.getView(R.id.item_downstair_type_parent);
        TextView type_name = helper.getView(R.id.item_downstair_type_name);
        TextView type_count = helper.getView(R.id.item_downstair_type_count);
//        if (entity.getCount() > 0) {
//            type_count.setText(entity.getCount() + "");
//            type_count.setVisibility(View.VISIBLE);
//        } else {
//            type_count.setVisibility(View.GONE);
//        }
        type_name.setText(entity.getName());
        if (entity.isChecked()) {
            main_ll.setBackgroundResource(R.drawable.stair_type_red);
            type_name.setTextColor(mContext.getResources().getColor(R.color.font_black));
            type_name.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            main_ll.setBackgroundResource(R.drawable.stair_type_gray);
            type_name.setTextColor(mContext.getResources().getColor(R.color.font_gary));
            type_name.setTypeface(Typeface.DEFAULT);
        }
        type_name.setOnClickListener(v -> {
            for (int i = 0; i < mData.size(); i++) {
                mData.get(i).setChecked(false);
            }
            mData.get(helper.getAdapterPosition()).setChecked(true);
            clickListener.itemClickListener(entity.getId());
            notifyDataSetChanged();
        });
    }

    public void setChick(int position) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChecked(false);
        }
        mData.get(position).setChecked(true);
        notifyDataSetChanged();
    }

    public void setChickForId(String id) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChecked(false);
        }
        for (int i = 0; i < mData.size(); i++) {
            if (id.equals(mData.get(i).getId())) {
                mData.get(i).setChecked(true);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public interface onItemClickListener {
        void itemClickListener(String id);
    }

    public void setClickListener(onItemClickListener clickListener) {
        this.clickListener = clickListener;
    }
}
